// pages/order/index.js
import { request } from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    tabs: [
      {
        id: 0,
        value: "全部订单",
        isActive: true
      },
      {
        id: 1,
        value: "代付款",
        isActive: false
      },
      {
        id: 2,
        value: "代发货",
        isActive: false
      },
      {
        id: 3,
        value: "退款/退货",
        isActive: false
      }
    ]
  },
  onShow () {
    const token = wx.getStorageSync("token")
    if (!token) {
      wx.navigateTo({
        url: '/pages/auth/index',
      });
      return;
    }
    let pages = getCurrentPages();
    let currentPages = pages[pages.length - 1];
    const { type } = currentPages.options;
    this.changeTitleTap(type - 1);
    console.log(type)
    this.getOrders(type);
  },
  async getOrders (type) {
    const res = await request({ url: "/my/orders/all", data: { type } })
    console.log(res)
    this.setData({
      orders: res.orders.map(v => ({ ...v, create_time_cn: (new Date(v.create_time * 1000).toLocaleString()) }))
    })
  },
  changeTitleTap (index) {
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    this.setData({
      tabs
    })
  },
  handleTabsItemChange (e) {
    const { index } = e.detail;
    this.changeTitleTap(index);
    this.getOrders(index + 1)
  },
})